// pages/customized/customized.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    babyNum:1,
    adultNum:1,
    TravelNum:1,
    travel:[
      {wayName:'飞机'},
      {wayName:'动车'},
      {wayName:'高铁'},
      {wayName:'火车'},
      {wayName:'大巴'}
    ],
    sexData: [
      {name:'男'},
      {name:'女'}
    ],
    travelVal:1,
    activeType:0,
    wantPlace:"",
    depCity:"",
    date:'请选择时间',
    sexActive: 0,
    contact_name:"",
    contact_company:"",
    contact_phone:'',
    contact_time:'',
    other_question: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tel: wx.getStorageSync('phoneNumber')
    })
  },
  wantPlace:function(e) {
    var val = e.detail.value
    this.setData({
      wantPlace:val
    })
  },
  depCity:function(e) {
    var val = e.detail.value
    this.setData({
      depCity:val
    })
  },
  selectSex:function(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      sexActive:index
    })
  },
  selectTravelWay:function(e) {
    var index = e.currentTarget.dataset.index
    var travelVal= e.currentTarget.dataset.value
    this.setData({
      activeType:index,
      travelVal:travelVal
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  userName:function(e) {
    var val = e.detail.value
    this.setData({
      contact_name:val
    })
  },
  company:function(e) {
    var val = e.detail.value
    this.setData({
      contact_company:val
    })
  },
  mobile:function(e) {
    var val = e.detail.value
    this.setData({
      contact_phone:val
    })
  },
  contactTime:function(e) {
    var val = e.detail.value
    this.setData({
      contact_time:val
    })
  },
  other:function(e) {
    var val = e.detail.value
    this.setData({
      other_question:val
    })
  },
  babyAdd:function(e) {
    var getObj = e.currentTarget.dataset.obj
    var _this = this
    var obj = {}
      obj[getObj] = _this.data[getObj] + 1
      this.setData(obj)
 

  },
  babySum:function(e) {
    var getObj = e.currentTarget.dataset.obj
    var _this = this
    var obj = {}
    if (_this.data[getObj]<2) {
      return
    } else {
      obj[getObj] = _this.data[getObj] - 1
      this.setData(obj)
    }
   
  },
  submit:function(e) {
    var _this = this
    var tel = /^1[3|4|5|6|7|8|9]\d{9}$/
    if (_this.data.wantPlace=="") {
      wx.showModal({
        title: '',
        content: '想要的定制不能为空',
      
      })
    } else if (_this.data.depCity=="") {
      wx.showModal({
        title: '',
        content: '想要的定制不能为空',
      
      })
    } else if (_this.data.date == "请选择时间") {
      wx.showModal({
        title: '',
        content: '请选择时间',
      
      })
    } else if (_this.data.contact_name == "") {
      wx.showModal({
        title: '',
        content: '请填写姓名',
      
      })
    } else if (_this.data.contact_company == "") {
      wx.showModal({
        title: '',
        content: '请填写企业名称',
      })

    } else if (!tel.test(_this.data.contact_phone)) {
      wx.showModal({
        title: '',
        content: '请输入正确的手机号',
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: 'https://miniapp.odeman.com/summercamp/customized.php', 
        data: {
          description: _this.data.wantPlace,
          city: _this.data.depCity,
          time:_this.data.date,
          adult_amount:_this.data.adultNum,
          child_amount:_this.data.babyNum,
          out_method:_this.data.travelVal,
          play_time:_this.data.TravelNum,
          contact_name:_this.data.contact_name,
          contact_company:_this.data.contact_company,
          contact_sex:_this.data.sexActive,
          contact_phone:_this.data.contact_phone,
          contact_time:_this.data.contact_time,
          other_question:_this.data.other_question
        },
        mthod:'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success (res) {
          wx.hideLoading()
          if (res.data.status == "success") {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 3000
            })
          } else {
            wx.showModal({
              title: '',
              content: res.data.message
            })
          }
        }
      })
    }
  }
})